package com.aiatomic.couting.settings;

import java.io.File;
import java.util.Calendar;

import com.aiatomic.couting.IConstant;
import com.aiatomic.couting.R;
import com.aiatomic.couting.SettingActivity;
import com.aiatomic.couting.util.SystemUtil;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class FragmentSettingStore extends PreferenceFragment {
	private SettingActivity activity;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		activity = (SettingActivity) this.getActivity();

		TextView txtBack = (TextView) activity.findViewById(R.id.txtBack);
		txtBack.setText(R.string.bottom_setup);
		txtBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getActivity().onBackPressed();
			}
		});
		TextView txtNext = (TextView) activity.findViewById(R.id.txtNext);
		txtNext.setVisibility(View.INVISIBLE);

		TextView txtMiddle = (TextView) activity.findViewById(R.id.txtMiddle);
		txtMiddle.setText(R.string.title_activity_store);
		addPreferencesFromResource(R.xml.save_preferences);

		EditTextPreference folder_name = (EditTextPreference) findPreference(IConstant.DEVICE_FOLDER_NAME);
		if (folder_name != null) {
			folder_name.setDefaultValue(SystemUtil.getSystemModel());
			SharedPreferences share = PreferenceManager.getDefaultSharedPreferences(activity);
			folder_name.setText(share.getString(IConstant.DEVICE_FOLDER_NAME, SystemUtil.getSystemModel()));
		}
		folder_name.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				Preference folder_structure = findPreference(IConstant.FOLDER_STRUCTURE2);
				if (folder_structure != null) {
					SharedPreferences share = PreferenceManager.getDefaultSharedPreferences(activity);
					folder_structure.setSummary(getString(R.string.folder_structure_options)+getFolderStructure(share.getString(IConstant.FOLDER_STRUCTURE2, "Day"),(String)newValue));
				}
				return true;
			}
		});
		Preference folder_structure = findPreference(IConstant.FOLDER_STRUCTURE2);
		if (folder_structure != null) {
			SharedPreferences share = PreferenceManager.getDefaultSharedPreferences(activity);
			folder_structure.setSummary(getString(R.string.folder_structure_options)+getFolderStructure(share.getString(IConstant.FOLDER_STRUCTURE2, "Day"),share.getString(IConstant.DEVICE_FOLDER_NAME, SystemUtil.getSystemModel())));
		}
		folder_structure.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				SharedPreferences share = PreferenceManager.getDefaultSharedPreferences(activity);

				String folderStruct = (String) newValue;
				preference.setSummary(getString(R.string.folder_structure_options)+getFolderStructure(folderStruct,share.getString(IConstant.DEVICE_FOLDER_NAME, SystemUtil.getSystemModel())));
				return true;
			}
		});

	}

	public String getFolderStructure(String folderStruct,String deviceFolder) {
		Calendar now = Calendar.getInstance();
		String storePath = Environment.getExternalStorageDirectory() + File.separator + getString(R.string.app_name)
				+ File.separator + deviceFolder;
		if (folderStruct.equals("Day")) {
			storePath += File.separator + String.valueOf(now.get(Calendar.YEAR)) + File.separator
					+ String.valueOf(now.get(Calendar.MONTH) + 1) + File.separator
					+ String.valueOf(now.get(Calendar.DAY_OF_MONTH)) + File.separator ;
		}
		if (folderStruct.equals("Month")) {
			storePath += File.separator + String.valueOf(now.get(Calendar.YEAR)) + File.separator
					+ String.valueOf(now.get(Calendar.MONTH) + 1) + File.separator ;
		}
		if (folderStruct.equals("Year")) {
			storePath += File.separator + String.valueOf(now.get(Calendar.YEAR)) + File.separator ;
		}
		if (folderStruct.equals("Root")) {
			storePath += File.separator;
		}
		return storePath;
	}
}
